#include "ConfigSlaveWork.h"
//#include "DAOProxy.h"
//#include "ConfigSlice.h"
#include "ConfigSlaveI.h"
#include "ConfigTimerWork.h"
#include "ConfigListenWork.h"
#include <iostream>
using namespace std;

namespace Config
{

ConfigSlaveActivity::ConfigSlaveActivity() :_activity(this,&ConfigSlaveActivity::runActivity)
{

}

void ConfigSlaveActivity::start()
{
	_activity.start();
}

void ConfigSlaveActivity::stop()
{
	_activity.stop();
	_activity.wait();
}

void ConfigSlaveActivity::runActivity()
{
	do
	{
		Ice::CommunicatorPtr ic;
	    //ic->load("config.client");
	 try{
		/*		ic = Ice::initialize(argc,argv);
		ic->getProperties()->load("config.slave")*/;
		Ice::InitializationData initData;
		initData.properties = Ice::createProperties();
		initData.properties->load("config.slave");
		ic = Ice::initialize(initData);
		//ConfigMasterPrx master = ConfigMasterPrx::checkedCast(ic->propertyToProxy("ConfigMaster.Proxy")->ice_timeout(-1));
		//if(!master)
		//{
		//	cerr << " [ConfigSlaveWork::run] invalid proxy [master]" << endl;
		//	exit(1);
		//}

		Ice::ObjectAdapterPtr adapter = ic->createObjectAdapter("ConfigSlave.Slave");
		ConfigSlaveI *        configslaveIptr = new ConfigSlaveI;
		ConfigSlavePtr        slaveptr = configslaveIptr;
		adapter->add(slaveptr, ic->stringToIdentity("ConfigSlave"));
		adapter->activate();

		ConfigSlavePrx configslaveprx = ConfigSlavePrx::uncheckedCast(adapter->createProxy(ic->stringToIdentity("ConfigSlave")));

		ConfigTimerWork timerwork(configslaveprx,"C001I001LogCom",10,5000);

		ConfigListenActivity listenwork(configslaveIptr);

		timerwork.start();
		listenwork.start();
		ic->waitForShutdown();
		timerwork.stop();
		listenwork.stop();
	}
	catch (const Ice::Exception& ex)
	{
		cerr<<"catch exception "<<ex.what()<<endl;
	}
	}while(!_activity.isStopped());
}

}//namespace Config